﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Dextrys.SharePoint;
using Higer.SharePoint.HigerWebService;

namespace Higer.SharePoint.UserControls
{
    public partial class moreWaitHandle : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            try
            {
                //DataTable dt_WaitHandle = GetWaitHandles("zhouhx");
                DataTable dt_WaitHandle = GetWaitHandles(GetCurrentUserName(this.CurrentUser.LoginName)); 
            
                if (dt_WaitHandle != null && dt_WaitHandle.Rows.Count > 0)
                {
                    gvWaitHandle.DataSource = dt_WaitHandle;
                    gvWaitHandle.DataBind();
                    gvWaitHandle.Visible = true;
                    lblMessage.Visible = false;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
                gvWaitHandle.Visible = false;
            }

            //}

        }

        #region 属性

        #endregion

        #region 方法

        public string GetCurrentUserName(string loginName)
        {
            if (loginName.Contains('\\'))
            {
                loginName = loginName.Substring(loginName.IndexOf('\\') + 1);
            }
            return loginName;
        }

        public DataTable GetWaitHandles(string username)
        {
            HigerWorkList higerWorkList = new HigerWorkList();
            DataSet ds = higerWorkList.GetWaitHandles(username);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }

       

        public string GetSubjectText(string subjectText)
        {
            if (subjectText.Contains("&#60;"))
            {
                subjectText = subjectText.Replace("&#60;", "<");
            }
            if (subjectText.Contains("&#62;"))
            {
                subjectText = subjectText.Replace("&#62;", ">");
            }

            return subjectText;
        }

        public string GetTaskTitle(string tasktitle, int length)
        {
            string resulttitle = "";
            tasktitle = GetSubjectText(tasktitle);
            resulttitle = GetStringByLength(tasktitle, length);

            return resulttitle;
        }

        /// <summary>
        /// 裁减字符串到指定的大小
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private string GetStringByLength(string str, int len)
        {
            bool fDBCSStarting = false;

            byte[] strArray = System.Text.Encoding.Default.GetBytes(str);
            if (strArray.Length <= len)
            {
                return str;
            }
            else
            {
                len = len - 4;
                byte[] ret = new byte[len + 1];
                for (int i = 0; i < len; i++)
                {
                    ret[i] = strArray[i];
                    // determine if the current byte is the starting byte of a DBCS character
                    if (ret[i].CompareTo((byte)0x7f) > 0)
                        fDBCSStarting = !fDBCSStarting;
                }


                if (fDBCSStarting == true || strArray[len].CompareTo((byte)0x80) < 0)
                    // read the ending byte
                    ret[len] = strArray[len];
                else
                    ret[len] = (byte)'.';

                return System.Text.Encoding.Default.GetString(ret) + "...";
            }
        }

        #endregion

        #region 事件

        #endregion
    }
}